/*
 * @Author: gy
 * @Date: 2019-12-17 09:57:18
 * @LastEditors  : wl
 * @LastEditTime : 2020-07-02 00:05:04
 */
import {
  getAction
} from '@/api/manage'
// import { delete } from 'vue/types/umd';
export default {
  data() {
    return {
      uid: '',
      loginName: ''
    }
  },
  created() {
    if (localStorage.getItem('userInfo')) {
      this.uid = JSON.parse(localStorage.getItem('userInfo'))['profile']['UserId']
    }
    if (localStorage.getItem('userInfo')) {
      this.loginName = JSON.parse(localStorage.getItem('userInfo'))['profile']['LoginName']
    }
  },
  mounted() { },
  methods: {
    // 唯一性检测
    async checkUnique(url, param) {
      let result = false;
      clearTimeout(this.setTime);
      result = await new Promise(resolve => {
        this.setTime = setTimeout(() => {
          getAction(url, param).then(res => {
            console.log(res, 'checkUnique')
            if (res.code === 0) {
              if (res.data === '0') {
                resolve(false)
              } else {
                resolve(true)
              }
            }
          })
        }, 500)
      })
      return result
    },
    // 数据结构的组装
    assembleTreeData(arr, id, pId) {
      let treeData = [];
      if (arr instanceof Array && arr.length) {
        // 删除所有的children,以防止多次调用
        arr.forEach(function (item) {
          delete item.children;
        });
        let map = {}; //构建map
        arr.forEach(i => {
          map[i[id]] = i; //构建以id为键 当前数据为值
        });
        // let treeData = [];
        arr.forEach(child => {
          const mapItem = map[child[pId]]; //判断当前数据的parentId是否存在map中
          if (mapItem) {
            //存在则表示当前数据不是最顶层的数据
            //注意： 这里的map中的数据是引用了arr的它的指向还是arr,当mapItem改变时arr也会改变，踩坑点
            (mapItem.children || (mapItem.children = [])).push(child); //这里判断mapItem中是否存在child
          } else {
            //不存在则是顶层数据
            treeData.push(child);
          }
        });
      } else {
        treeData = []
      }
      return treeData
    },
    // tree数据组装-存在顶级0
    assembleTreeDataDept(arr) {
      let brr = [];
      if (arr instanceof Array && arr.length) {
        arr.forEach(item => {
          if(item.pId === '0') {
            brr.push(item)
          }
          item.children = arr.filter(items => items.pId === item.id)
        })
      }
      return brr;
    },
    // 处理数据结构
    handleTreeData(data) {
      if (data instanceof Array && data.length) {
        data.map(item => {
          if (item.children && item.children.length) {
            this.handleTreeData(item.children)
          } else {
            delete item['children']
          }
        })
      }
      return data
    }
  }
}
